package smb;

import java.io.*;
import java.net.*;
import java.util.*;
		
public class Stat {
	
	private Map<String, Integer> serversdb = new TreeMap<String, Integer>();
	private Map<String, Integer> usersdb = new TreeMap<String, Integer>();
	private boolean timeout = true;
	
    private DatagramSocket socket = null;
    
	public static void main (String[] args) throws IOException{
		int STATport=59000;
		
		if(args.length>0){
			if(args[1].equals("-t")){
				try {
				STATport=Integer.parseInt(args[2]);
				} catch (NumberFormatException e) {
					System.err.println("Command line \"-t\" argument must be an integer");
					System.exit(-1);
				}
			}
		}
		
		Stat statserver= new Stat(STATport);
		
		statserver.run();
	}
	
	public Stat(int port) throws IOException{
		socket=new DatagramSocket(port);	
	}
	
	public void run(){		
		try{
			while(timeout){
				try {
				    byte[] buf = new byte[256];
				
				// receive request
				    DatagramPacket packet = new DatagramPacket(buf, buf.length);
				    socket.receive(packet);
				
				// figure out response
				    Scanner scanner = new Scanner( new String(packet.getData(), 0, packet.getLength()));
				    String message = scanner.nextLine();
				    String[] temp;
				    String delimiter = " ";
				    temp = message.split(delimiter);
				    if(temp[0].equals("UPD")){
				    	message = this.update(temp);
				    }else{ 
				    	if(temp[0].equals("REG")){
				    		message = this.register(temp);
				    	}else
				    		message = "Erro\n";
				    }
				    buf = message.getBytes();
				
				// send the response to the client at "address" and "port"
			        InetAddress address = packet.getAddress();
			        int port = packet.getPort();
			        packet = new DatagramPacket(buf, buf.length, address, port);
			        socket.send(packet);
			    } catch (IOException e){
			    	
			    }
			}
			
		} finally {
			if(socket != null)socket.close();
		}		
		
	}
	
	public String update(String[] tokens){
		String status = new String("Server: "),
				key = tokens[1]+":"+tokens[2];

		if(!serversdb.containsKey(key))
			serversdb.put(key, 0);

		int update = serversdb.get(key)+1;
		serversdb.put(key, update);
			
		if(!usersdb.containsKey(tokens[3]))
			usersdb.put(tokens[3], 0);

		update = usersdb.get(tokens[3])+1;
			usersdb.put(tokens[3], update);
		
		status += tokens[1] + " " + tokens[2] + " - #" + serversdb.get(key);
		System.out.println(status);
		
		status = "Client: " + tokens[3] + " - #" + usersdb.get(tokens[3]);
		System.out.println(status);
		
		status = "Ok\n";
		return status;
	}
	
	public String register(String[] tokens){
		String status = new String("Server: "),
				key = tokens[1]+":"+tokens[2];
		
		if(!serversdb.containsKey(key))
			serversdb.put(key, 0);
		
		status += tokens[1] + " " + tokens[2] + " - #" + serversdb.get(key);
		
		System.out.println(status);
		status = "Ok\n";
		
		return status;  
		
	}
}
